﻿Imports System.IO

Public Class Images

   Shared Images As New SortedList(Of String, BitmapList)
   Public Shared Stack As New List(Of File)

   Shared Sub StartProcessing()
      For Each File In Studio.Files
         Stack.Add(File)
      Next
      FrmMain.ProgressBar.Maximum = Stack.Count
      ProcessQueue(Nothing)
      ProcessQueue(Nothing)
   End Sub

   Shared Sub ProcessQueue(ByVal File As File)
      If Not File Is Nothing Then
         Studio.Invalidate(File.Name)
      End If
      FrmMain.ProgressBar.Increment(1)
      If Stack.Count = 0 Then
         FrmMain.PanelBar.Visible = False
         Return
      End If
      Dim Item = Stack.First
      Stack.RemoveAt(0)
      AsyncRun(AddressOf ProcessFile, AddressOf ProcessQueue, Item)
      AsyncRun(AddressOf ProcessFile, AddressOf ProcessQueue, Item)
      'ProcessFile(Item)
      'ProcessQueue(Item)
   End Sub

   Shared Sub ProcessFile(ByVal File As File)
      Try
         If File.Name.ToLower.EndsWith(".jpg") Then
            If File.Width = "" Then
               Dim Stream As FileStream = System.IO.File.OpenRead(File.CentralName(2))
               Dim Image As Image = Image.FromStream(Stream, False, False)
               File.Width = Image.Width
               File.Height = Image.Height
               Stream.Close()
               Image.Dispose()
               Studio.Files.Save()
            End If
            If Not My.Computer.FileSystem.FileExists(File.LocalName(1)) Then
               If Not My.Computer.FileSystem.FileExists(File.CentralName(1)) Then
                  Dim B1 = Image.FromFile(File.CentralName(2))
                  Dim B2 = New Bitmap((B1.Width * 1080) \ B1.Height, 1080)
                  Dim G = Graphics.FromImage(B2)
                  G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
                  G.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
                  G.DrawImage(B1, New Rectangle(0, 0, B2.Width, B2.Height))
                  B1.Dispose()
                  B2.Save(File.LocalName(1), System.Drawing.Imaging.ImageFormat.Jpeg)
                  B2.Dispose()
                  My.Computer.FileSystem.CopyFile(File.LocalName(1), File.CentralName(1))
               Else
                  My.Computer.FileSystem.CopyFile(File.CentralName(1), File.LocalName(1))
               End If
            End If
            If Not My.Computer.FileSystem.FileExists(File.LocalName(0)) Then
               If Not My.Computer.FileSystem.FileExists(File.CentralName(0)) Then
                  Dim Img = Image.FromFile(File.CentralName(2))
                  For Each Item In Img.PropertyItems
                     If Item.Id = 20507 Then
                        Dim Buffer = Item.Value
                        Dim ThumbStream As New System.IO.MemoryStream(Buffer.Length)
                        ThumbStream.Write(Buffer, 0, Buffer.Length)
                        Dim Thumb = Image.FromStream(ThumbStream)
                        Thumb.Save(File.CentralName(0), System.Drawing.Imaging.ImageFormat.Jpeg)
                     End If
                  Next
               End If
               If Not My.Computer.FileSystem.FileExists(File.CentralName(0)) Then
                  Dim B1 = Image.FromFile(File.CentralName(2))
                  Dim B2 = New Bitmap((B1.Width * 240) \ B1.Height, 240)
                  Dim G = Graphics.FromImage(B2)
                  G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
                  G.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
                  G.DrawImage(B1, New Rectangle(0, 0, B2.Width, B2.Height))
                  B1.Dispose()
                  B2.Save(File.CentralName(0), System.Drawing.Imaging.ImageFormat.Jpeg)
                  B2.Dispose()
               End If
               My.Computer.FileSystem.CopyFile(File.CentralName(0), File.LocalName(0))
            End If
         End If
      Catch ex As Exception
      End Try
   End Sub

   Shared Sub DrawImage(ByVal G As Graphics, ByVal Rect As Rectangle, ByVal File As String, ByVal Height As Integer)
      If Not Images.ContainsKey(File) Then Images.Add(File, New BitmapList(Studio.Files(File)))
      Dim Img As Bitmap = Images(File).Get(Height)
      G.InterpolationMode = Drawing2D.InterpolationMode.Bilinear
      G.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
      If Img Is Nothing Then Return
      G.DrawImage(Img, Rect)
      Rect.Inflate(-8, -8)
      If Images(File).IsBusy Then
         DrawText(G, "İyileştiriliyor...", New Font("segoe ui", 18, FontStyle.Regular), Rect, Color.White, "rb")
      End If
   End Sub

   Shared Sub DrawThumb(ByVal G As Graphics, ByVal Rect As Rectangle, ByVal File As String, ByVal Selected As Boolean)
      Dim R = Rect
      R.Inflate(-(R.Width - R.Height) \ 2, 0)
      G.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
      If Not Images.ContainsKey(File) Then Images.Add(File, New BitmapList(Studio.Files(File)))
      Dim Img As Bitmap = Images(File).Get(R.Height)
      Dim IR = R
      IR.Inflate(-IR.Width / 15, -IR.Width / 15)
      Dim LR = IR
      LR.Height = R.Height - IR.Height
      LR.Offset(0, R.Bottom - LR.Bottom)
      LR.Offset(0, R.Bottom - LR.Bottom)
      If Img Is Nothing Then
         IR.Inflate(0, -IR.Height / 8)
         IR.Offset(0, LR.Top - IR.Bottom)
         G.FillRectangle(Brushes.Black, IR)
			TextRenderer.DrawText(G, "YÜKLENİYOR", New Font("segoe ui", LR.Height * 3 / 4), IR, Color.White, Color.Transparent, TextFormatFlags.HorizontalCenter + TextFormatFlags.VerticalCenter + TextFormatFlags.PreserveGraphicsTranslateTransform)
      Else
         If Img.Width > Img.Height Then
            IR.Inflate(0, -(IR.Height - IR.Height * Img.Height / Img.Width) \ 2)
         Else
            IR.Inflate(-(IR.Width - IR.Width * Img.Width / Img.Height) \ 4, 0)
         End If
         IR.Offset(0, LR.Top - IR.Bottom)
		End If
		IR.Width -= 1 : IR.Height -= 1
		Dim Path As New Drawing2D.GraphicsPath
		Path.AddRectangle(IR)
		Path.AddRectangle(LR)
		For i = 1 To 10
			Dim Pen As New Pen(Color.Black.Opacity(192 / (i ^ 2)), i)
			Pen.LineJoin = Drawing2D.LineJoin.Round
			G.DrawPath(Pen, Path)
		Next
		If Not Img Is Nothing Then G.DrawImage(Img, IR, New Rectangle(0, 0, Img.Width, Img.Height), GraphicsUnit.Pixel)
		If Selected Then
			IR.Height += 2
			G.DrawRectangle(SystemColors.Highlight.Pen, IR)
			IR.Inflate(-1, -1)
			G.DrawRectangle(SystemColors.Highlight.Pen, IR)
			IR.Inflate(-1, -1)
			G.DrawRectangle(Color.White.Opacity(128).Pen, IR)
		Else
			'G.DrawRectangle(Color.Black.Opacity(64).Pen, IR)
			'R.Inflate(-1, -1)
			'G.DrawRectangle(Color.White.Opacity(32).Pen, IR)
		End If
      G.FillRectangle(If(Selected, SystemBrushes.Highlight, Brushes.Black), LR)
		TextRenderer.DrawText(G, File.Replace(".JPG", ""), New Font("segoe ui", LR.Height * 2 / 4), LR, Color.White, Color.Transparent, TextFormatFlags.HorizontalCenter + TextFormatFlags.VerticalCenter + TextFormatFlags.PreserveGraphicsTranslateTransform)
   End Sub

End Class

Class BitmapList

   Dim Bitmaps As New List(Of BitmapInfo)
   Dim File As File

   Sub New(ByVal File As File)
      Me.File = File
   End Sub

   Sub GetImage(ByVal Info As BitmapInfo)
      Try
         Select Case Bitmaps.Count
            Case 0
               If My.Computer.FileSystem.FileExists(File.LocalName(0)) Then
                  Info.Bitmap = Image.FromFile(File.LocalName(0))
               End If
            Case 1
               If My.Computer.FileSystem.FileExists(File.LocalName(1)) Then
                  Info.Bitmap = Image.FromFile(File.LocalName(1))
               End If
            Case 2
               If My.Computer.FileSystem.FileExists(File.LocalName(2)) Then
                  Info.Bitmap = Image.FromFile(File.LocalName(2))
               End If
         End Select
      Catch ex As Exception
      End Try
   End Sub

   Sub ImageGot(ByVal Info As BitmapInfo)
      IsBusy = False
      If Info.Bitmap Is Nothing Then Return
      Bitmaps.Add(Info)
      Studio.Invalidate(File.Name)
   End Sub

   Friend IsBusy As Boolean

   Function [Get](ByVal Height As Integer) As Bitmap
      If Images.Stack.Contains(File) Then
         Images.Stack.Remove(File)
         Images.Stack.Insert(0, File)
      End If
      Dim Info = (From b In Bitmaps Where b.Bitmap.Height > Height Order By Height).FirstOrDefault
      If Info Is Nothing Then
         Info = (From b In Bitmaps Order By Height Descending).FirstOrDefault
      End If
      Dim h = If(Info Is Nothing, 0, Info.Bitmap.Height)
      If h < Height And Not IsBusy Then
         IsBusy = True
         Dim i As New BitmapInfo
         AsyncRun(AddressOf GetImage, AddressOf ImageGot, i)
      End If
      If Info Is Nothing Then Return Nothing
      Return Info.Bitmap
   End Function

End Class

Class BitmapInfo

   Public Bitmap As Bitmap
   Public LastUsed As DateTime

End Class